package io.itit.ecp.admin.provide.action.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.*;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.ecp.admin.provide.action.req.DataRangeSceneQueryReq;
import io.itit.ecp.admin.provide.action.req.DataRangeSceneReq;
import io.itit.ecp.admin.provide.action.resp.DataRangeSceneResp;
import io.itit.ecp.admin.provide.renderer.DataRangeSceneRenderer;
import io.itit.ecp.admin.server.entity.DataRangeSceneEntity;
import io.itit.ecp.admin.server.service.IDataRangeSceneService;
import io.itit.grass.estelle.starter.core.ExcelXSSFUtil;
import io.itit.grass.estelle.starter.parse.ExcelParseManage;
import io.itit.grass.estelle.starter.util.JsonUtil;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：数据范围场景关联控制类
 * @Date：Create by 2024-01-09 11:37:16
 */
@Api(tags = "数据范围场景关联")
@RestController
@RequestMapping("/ecp-admin/dataRangeScene")
public class DataRangeSceneController {

	@Resource
	private IDataRangeSceneService dataRangeSceneService;

	@ApiOperation("分页查询")
	@PostMapping("queryPage")
	@RequiresPermissions("ecp-admin:dataRangeScene:search")
	public ResultResp<IPage<DataRangeSceneResp>> queryPage(@RequestBody @Validated(IQueryPage.class) DataRangeSceneQueryReq req) {
		Page<DataRangeSceneEntity> page = dataRangeSceneService.page(PageUtils.getPage(req, DataRangeSceneEntity.class), buildQW(req));
		return ResultResp.success(ConvertUtils.convert(page, DataRangeSceneResp.class));
	}

	@ApiOperation("详情")
	@PostMapping("detail")
	@RequiresPermissions("ecp-admin:dataRangeScene:search")
	public ResultResp<DataRangeSceneResp> detail(@RequestBody @Validated(IRequired.class) BaseIdReq req) {
		return ResultResp.success(ConvertUtils.copy(dataRangeSceneService.getById(req.getId()), DataRangeSceneResp.class));
	}

	/**
     * 组装查询条件
     *
     * @param req
     * @return
     */
	private static LambdaQueryWrapper<DataRangeSceneEntity> buildQW(DataRangeSceneQueryReq req) {
		return Wrappers.<DataRangeSceneEntity>lambdaQuery()
				.eq(StringUtils.isNotEmpty(req.getDataRangeId()), DataRangeSceneEntity::getDataRangeId, req.getDataRangeId())
				.eq(StringUtils.isNotEmpty(req.getSceneId()), DataRangeSceneEntity::getSceneId, req.getSceneId())
				.eq(StringUtils.isNotEmpty(req.getDeptId()), DataRangeSceneEntity::getDeptId, req.getDeptId())
				.in(CollectionUtils.isNotEmpty(req.getIds()), DataRangeSceneEntity::getId, req.getIds())
				.last(StringUtils.isNotEmpty(req.getSortColumn()), PageUtils.lastOrderBy(req))
				.orderByDesc(StringUtils.isEmpty(req.getSortColumn()), DataRangeSceneEntity::getCreateTm);
	}

	@ApiOperation("新增")
	@PostMapping("add")
	@RequiresPermissions("ecp-admin:dataRangeScene:add")
	public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) DataRangeSceneReq req) {
		return ResultResp.success(dataRangeSceneService.save(ConvertUtils.convertIgnoreId(req, DataRangeSceneEntity.class)));
	}

	@ApiOperation("编辑")
	@PostMapping("update")
	@RequiresPermissions("ecp-admin:dataRangeScene:update")
	public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) DataRangeSceneReq req) {
		return ResultResp.success(dataRangeSceneService.updateById(ConvertUtils.copy(req, DataRangeSceneEntity.class)));
	}

	@ApiOperation("删除(根据Id删)")
	@PostMapping("deleteById")
	@RequiresPermissions("ecp-admin:dataRangeScene:deleteById")
	public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
		return ResultResp.success(dataRangeSceneService.removeById(req.getId()));
	}

	@ApiOperation("批量删除")
	@PostMapping("batchDelete")
	@RequiresPermissions("ecp-admin:dataRangeScene:batchDelete")
	public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
		return ResultResp.success(dataRangeSceneService.removeBatchByIds(req.getIds(), 1000));
	}

	@ApiOperation("导入")
	@PostMapping("upload")
	@RequiresPermissions("ecp-admin:dataRangeScene:upload")
	public ResultResp<Integer> upload(@RequestParam("file") MultipartFile file, Boolean ignoreError) {
		return ResultResp.success(ExcelParseManage.i().process(DataRangeSceneRenderer.ENTITY_NAME, file, ignoreError));
	}

	@ApiOperation("导出")
	@PostMapping("export")
	@RequiresPermissions("ecp-admin:dataRangeScene:export")
	public void export(@RequestBody @Validated(IExport.class) DataRangeSceneQueryReq req, HttpServletResponse response) {
		ExcelXSSFUtil.export("DataRangeSceneExport", JsonUtil.toJson(dataRangeSceneService.list(buildQW(req))), response);
	}
}